(function ($) {
	var sourceData;
	var nurseId=''
	var unUploadFiles=[] //未上传的图片
	var wardId = ''
	var keysArr = keysArr = [{'key':'EytHire','type':'list','text':'职称'},{'key':'EytTitle','type':'input','text':'论文题目'},
				{'key':'EytType','type':'list','text':'论文类型','isMust':0}]
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//护士id^层级^病区^职称^论文题目^ 论文类型^id^状态
	function submitNewData(){
		blurAllInput()
		var status = this.getAttribute('data-id');
		var level =  $('#EytLevel')[0].getAttribute('data-id');
		if (!level || level == '') {
			mui.toast('请选择层级')
			showEmptyAnimation('EytLevel')
			return;
		}
		var parr = nurseId+'^'+level+'^'+wardId
		for (var i = 0; i < keysArr.length;i++) {
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var id = oneKey['key']
			var dom = $('#'+id)[0];
			var tip = '请选择'
			var str = ''
			if (type == 'authType') { //作者
				str = dom.getAttribute('data-id')
			}else if (type == "input"){
				str = dom.value
				tip = '请输入'
			}else if (type == 'date') {
				str = dom.innerText
			}else{
				str = dom.getAttribute('data-id')
			}
			if (str == '' && oneKey['isMust']!='0') {
				mui.toast(tip+oneKey['text'])
				showEmptyAnimation(id)
				return;
			}
			parr +='^'+str
		}
		parr += '^'
		if (sourceData) {
			parr += sourceData['rw'];
		}
		parr += '^'+status;
		//console.log(parr)
		var pdict = {'nurseid':nurseId,'parr':parr}
		var paramDict = {'method':'web.INMSMComm:SaveEndYearThseis','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msg = parseInt(result['msg'])
			if (msg == 0) {
				mui.toast('提交失败')
				return 
			}
			if (unUploadFiles.length==0 ) {
				plus.webview.getWebviewById('../WKPlatformPages/ky/keyanList.html').evalJS('detailChanged()')
				mui.back()
				return;
			}
			var path = getUploadFileToServerPath('上交论文备案')
			var method = getSaveFileMethod('上交论文备案')
			var paramDict={'id':msg,'path':path,'imgArr':unUploadFiles,'method':method}
			saveFileAction(paramDict,function(flag){
				if (!flag) {
					return
				}
				unUploadFiles=[]
				plus.webview.getWebviewById('../WKPlatformPages/ky/keyanList.html').evalJS('detailChanged()')
				mui.back()
			})
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function updateUI(){
		var nurLevel = $('#EytLevel')[0]
		nurLevel.innerText = sourceData['EytLevelDesc']
		nurLevel.setAttribute('data-id',sourceData['EytLevel'])
		for (var i = 0; i < keysArr.length;i++) {
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var id = oneKey['key']
			var dom = $('#'+id)[0];
			var valueStr = sourceData[id]==undefined?"":sourceData[id]
			if (type == 'authType') {
				dom.innerText = sourceData[id+'Name']
				dom.setAttribute('data-id',valueStr)
			}else if(type == "input"){
				dom.value = valueStr
			}else if (type == 'date') {
				dom.innerText = valueStr
			}else if (type == 'list'){
				dom.innerText = sourceData[id+'Desc']
				dom.setAttribute('data-id',valueStr)
			}
		}
		updateFileUI('EytFile','fileUL',sourceData,sourceData['rw'],getUploadFileToServerPath('上交论文备案'))
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		mui.previewImage();
		var self = plus.webview.currentWebview()
		sourceData = self.data
		var perInfo = self.perInfo
		nurseId = perInfo.loginID
		wardId = perInfo.loginWardId
		$('#CreatorName')[0].innerText = perInfo.loginName
		$('#applyLoc')[0].innerText = perInfo.loginWardDesc
		if (sourceData) { //修改
			updateUI()
		}
		//统计信息的关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		//第1作者 参与者
		mui('body').on('tap','.authType',function(){
			blurAllInput()
			var dom = this
			setTimeout(function() {
				toChosePerson(dom.id,'多选') //防止切换页面时键盘未隐藏
			}, 100);
		});
		//下拉列表
		mui('#UL').on('tap','.listType',downListElementTapped); //会议级别 是否本专科 获得奖项
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		window.addEventListener('resize',windowResize)
		$('#addFileBtn').on('change','input',function(){
			var imgArr = this.files
			unUploadFiles = afterSelectFile('fileUL',unUploadFiles,imgArr)
		});
		//监听文件删除按钮
		mui('#fileUL').on('tap','.mui-btn',function(){
			var method = getDeleteFileMethod('上交论文备案')
			var paramDict={'imgArr':unUploadFiles,'method':method,'nurseid':nurseId}
			deleteFileBtnTapped(this,paramDict,function(flag,arr){
				if (arr!=undefined) {
					unUploadFiles = arr
				}
			});
		}) 
		mui.back = function(){
			if (unUploadFiles.length > 0) {
				mui.confirm('您有未上传的文件','温馨提示',['放弃','继续操作'],function (e) {
					if (e.index == 0) {
						plus.nativeUI.closeWaiting()
						plus.webview.currentWebview().close()
					}
				},'div')
				return
			}
			plus.nativeUI.closeWaiting()
			plus.webview.currentWebview().close()
		}
	}
})(mui)

function selectPerson(domId,params){
	setAuthorDomInfo(domId,params)
}


/*
交流获奖论文
	{"EytNurse":"2","EytNurseName":"张芮菲","EytLevel":"19||1","EytLevelDesc":"N0","EytHire":"12||3","EytHireDesc":"主管护师",
	"EytWard":"1","EytWardDesc":"CCU","EytTitle":"年底上交论文测试1","EytType":"72||9","EytTypeDesc":"学位论文","EytStatus":"N",
	"EytStatusDesc":"已保存","Creator":"0","CreatorName":"管理员","CreateDate":"2022-02-12","EytFile":"","rw":"3"}
*/